package com.kitty.game.task.service.taskHandler;

import com.kitty.game.role.model.Role;
import com.kitty.game.config.NPC;
import com.kitty.game.config.TaskSet;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.task.message.RespTasksDel;
import com.kitty.game.task.message.vo.TaskInfo;
import com.kitty.game.task.model.product.TaskTarget;
import com.kitty.game.task.model.product.TaskTargetType;
import com.kitty.game.task.model.product.TaskType;
import com.kitty.game.task.model.user.RoleTask;
import com.kitty.game.task.service.NewTaskService;
import com.kitty.game.task.service.targetHandler.TaskTargetHelper;
import com.kitty.listener.EventDispatcher;
import com.kitty.listener.EventType;
import com.kitty.listener.event.FightEndEvent;
import com.kitty.listener.event.TaskEndEvent;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public abstract class TaskHandler {

    NewTaskService taskService = SpringUtils.getBean(NewTaskService.class);

    /**接取任务*/
    public boolean acceptTask(Role role, TaskSet taskSet) {
        if (!canAcceptTask(role, taskSet)) {
            return false;
        }

        doBeforeAccept(role, taskSet);

        doAcceptTask(role, taskSet);

        doAfterAccept(role, taskSet);

        return true;
    }

    /**检测能否接取任务*/
    public boolean canAcceptTask(Role role, TaskSet taskSet) {
        /**判断是否已经接取，已经接取当然就不能再接取了*/
        if (taskService.isTaskAccepted(role, taskSet)) {
            return false;
        }

        /** 判断前一任务是否已经完成*/
        if (!taskService.isPrevTaskFinished(role, taskSet)) {
            return false;
        }

        return true;
    }

    /**进行任务接取前的操作*/
    public void doBeforeAccept(Role role, TaskSet taskSet) {

    }

    /**进行任务的接取*/
    public void doAcceptTask(Role role, TaskSet taskSet) {
        RoleTask roleTask = role.getTask();
        roleTask.acceptTask(taskSet.getId());
        role.save();
//        logger.info("[TASK]|accept|{}|{}", role.getUid(), taskSet.getId());

        pushTask(role, taskSet);
        /**接取完后，执行任务目标相关的前置动作，比如刷新NPC*/
        TaskTarget taskTarget = taskService.getRoleDoingTaskTarget(role, taskSet);
        taskService.startTaskTarget(role, taskSet, taskTarget);
    }

    /**进行任务接取后的操作*/
    public void doAfterAccept(Role role, TaskSet taskSet) {
        /**如果是学习技能那个任务的话 发一下指引*/
        if(taskSet.getId()==24){
            taskService.pushGuide(role, (short) (role.getExtendBox().getAutoAttrib().get("type")==2?30:31));
        }

    }

    public void pushTask(Role role, TaskSet taskSet) {
        /**增加try catch避免影响其他任务的显示*/
        try {
            TaskInfo taskInfo = getTaskInfo(role, taskSet);
            taskService.pushTaskInfo(role, taskInfo);
        } catch (Exception e) {
          //  log.error(e.getMessage(), e);
        }
    }

    public TaskInfo getTaskInfo(Role role, TaskSet taskSet) {
        return taskService.getTaskInfo(role, taskSet);
    }

    /**提交任务*/
    public boolean submitTask(Role role, TaskSet taskSet) {
        if (!canSubmitTask(role, taskSet)) {
            return false;
        }

        doBeforeSubmit(role, taskSet);

        doSubmiteTask(role, taskSet);

        doAfterSubmit(role, taskSet);

        /**产生一个任务完成的事件*/
        EventDispatcher.getInstance().fireEvent(new TaskEndEvent(EventType.TASK_END, role, taskSet.getId()));

        return true;
    }

    /**检测能否提交任务*/
    public boolean canSubmitTask(Role role, TaskSet taskSet) {
        return role.getTask().isCanSubmit(taskSet.getId());
    }

    public void doBeforeSubmit(Role role, TaskSet taskSet) {

    }

    /**进行任务的提交*/
    public void doSubmiteTask(Role role, TaskSet taskSet) {
        RoleTask roleTask = role.getTask();
        roleTask.updateToFinished(taskSet.getId());
        role.save();
//        logger.info("[TASK]|submit|{}|{}", role.getUid(), taskSet.getId());
    }

    /**进行任务提交后的操作*/
    public void doAfterSubmit(Role role, TaskSet taskSet) {
        acceptNextTask(role, taskSet);

        giveReward(role, taskSet);
    }

    /**接取下一任务*/
    public void acceptNextTask(Role role, TaskSet taskSet) {
        TaskSet nextTask = getNextTask(role, taskSet);

        if (isPushEnd(taskSet, nextTask)) {
            /**发隐藏包*/
            pushEndTask(role, taskSet);
        }

        /**接取下一任务*/
        if (nextTask != null) {
            acceptTask(role, nextTask);
        }
    }

    /**是否发送任务删除引导的包*/
    private boolean isPushEnd(TaskSet taskSet, TaskSet nextTask) {
        /**下一任务名称与当前任务名称不相同时，则需要处理*/
        if (nextTask == null || !nextTask.getTaskName().equals(taskSet.getTaskName())) {
            return true;
        }

        return false;
    }

    public void pushEndTask(Role role, TaskSet taskSet) {
        taskService.pushEndTask(role, taskSet.getTaskName());
    }

    /**获得下一任务*/
    public TaskSet getNextTask(Role role, TaskSet taskSet) {
        return taskService.getNextTask(taskSet, role);
    }

    /**给奖励*/
    public void giveReward(Role role, TaskSet taskSet) {
        taskService.giveAward(role, taskSet);
    }

    /**放弃任务*/
    public boolean giveUpTask(Role role, TaskSet taskSet) {
        if (!canGiveUpTask(role, taskSet)) {
            return false;
        }

        doBeforeGiveUpTask(role, taskSet);

        doGiveUpTask(role, taskSet);

        doAfterGiveUp(role, taskSet);

        return true;
    }

    /**能否放弃任务*/
    public boolean canGiveUpTask(Role role, TaskSet taskSet) {
        return taskSet.getGiveUp() == 1;
    }

    /**进行任务放弃前的操作*/
    public void doBeforeGiveUpTask(Role role, TaskSet taskSet) {
    }

    /**进行任务的放弃*/
    public void doGiveUpTask(Role role, TaskSet taskSet) {
        /**放弃*/
        role.getTask().giveUpTask(taskSet.getId());
        role.save();
        log.info("[TASK]|giveup|{}|{}", role.getUid(), taskSet.getId());
    }

    /**进行任务放弃后的操作*/
    public void doAfterGiveUp(Role role, TaskSet taskSet) {
        pushEndTask(role, taskSet);
    }


    /**重置任务*/
    public boolean resetTask(Role role, TaskSet taskSet) {
        if (!canResetTask(role, taskSet)) {
            return false;
        }

        doBoforeResetTask(role, taskSet);

        doResetTask(role, taskSet);

        doAfterReset(role, taskSet);

        return true;
    }

    /**强制重置任务*/
    public boolean forceResetTask(Role role, TaskSet taskSet) {
        doBoforeResetTask(role, taskSet);

        doResetTask(role, taskSet);

        doAfterReset(role, taskSet);
        return true;
    }

    /**能否重置任务*/
    public boolean canResetTask(Role role, TaskSet taskSet) {
        return false;
    }

    /**进行任务重置前的操作*/
    public void doBoforeResetTask(Role role, TaskSet taskSet) {
    }

    /**进行任务的重置*/
    public void doResetTask(Role role, TaskSet taskSet) {
        role.getTask().resetTask(taskSet.getId());
        role.save();
        log.info("[TASK]|reset|{}|{}", role.getUid(), taskSet.getId());
    }

    /**进行任务重置后的操作*/
    public void doAfterReset(Role role, TaskSet taskSet) {
        pushEndTask(role, taskSet);
    }

    /**获得该NPC上的任务按钮*/
    public String getTaskButtons(Role role, NPC npc) {
        return "";
    }

    public TaskSet getTaskSetByType(Role role) {
        List<TaskSet> acceptedTaskListMatchTaskType = taskService.getAcceptedTaskListMatchTaskType(role, getTaskType());
        if (acceptedTaskListMatchTaskType.isEmpty()) { return null; }
        return acceptedTaskListMatchTaskType.get(0);
    }

    public abstract TaskType getTaskType();

    public void doFightEnd(Role role, FightEndEvent fightEndEvent) {
        switch (fightEndEvent.getStatus()) {
            case FightEndEvent.WIN:
                doFightWin(role, fightEndEvent);
                break;
            case FightEndEvent.FAIL:
                doFightFail(role, fightEndEvent);
                break;
            case FightEndEvent.RUNAWAY:
                doFightRunAway(role, fightEndEvent);
                break;
            case FightEndEvent.MAXROUND:
                doFightMaxRound(role, fightEndEvent);
                break;
        }
    }

    public void doFightWin(Role role, FightEndEvent fightEndEvent) {
        TaskTargetHelper.INSTANCE.getTargetHandler(TaskTargetType.FIGHT_WIN).handleEvent(role, fightEndEvent);
    }

    public void doFightFail(Role role, FightEndEvent fightEndEvent) {
        /**判断战斗任务目标的PopType是否是NOW，如果是需要把当前任务的进度还原到不是NOW的目标上*/
        taskService.revertFightTaskTarget(role, fightEndEvent);
    }

    public void doFightRunAway(Role role, FightEndEvent fightEndEvent) {
        /**判断战斗任务目标的PopType是否是NOW，如果是需要把当前任务的进度还原到不是NOW的目标上*/
        taskService.revertFightTaskTarget(role, fightEndEvent);
    }

    public void doFightMaxRound(Role role, FightEndEvent fightEndEvent) {
        /**判断战斗任务目标的PopType是否是NOW，如果是需要把当前任务的进度还原到不是NOW的目标上*/
        taskService.revertFightTaskTarget(role, fightEndEvent);
    }

    public void handleLogin(Role role) {
    }

    /**获取目标NPC的按钮*/
    public String getButtonContents(Role role, String content) {return "";}
    /**是否可以删除任务*/
    public boolean isDeleteTask(Role role, TaskSet taskSet) {
        /**没有接任务时，则返回不用删除*/
        return SpringUtils.getBean(NewTaskService.class).isTaskAccepted(role, taskSet);
    }

    public void deleteTask(Role role,TaskSet taskSet){
        if (!isDeleteTask(role, taskSet)) {return ;}

        /**最后才删除任务数据*/
//        role.getTask().deleteTask(taskSet.getId());
        role.getTask().resetTask(taskSet.getId());
        role.save();

        /**隐藏显示*/
        RespTasksDel respTasksDel = new RespTasksDel();
        respTasksDel.setList(new ArrayList<>());
        respTasksDel.getList().add(taskSet.getTaskName());
        MessagePusher.pushMessage(role, respTasksDel);
    }

}
